<?php

namespace App\Http\Controllers;

use App\Libs\AudioTemplateConf;
use App\Libs\BaiduTtsApi;
use App\Libs\Curl;
use App\Libs\Interfaces\BaiduTts;
use App\Libs\Interfaces\Demo;
use App\Models\TradeDetail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class TestController extends Controller
{
    public function test(BaiduTts $tts)
    {
        $template_info = AudioTemplateConf::TEMPLATE['10001'];
        $txt = $template_info['text'];

        $params = ['amt'=>'20'];
        foreach ($params as $key=>$param){
            $txt = str_replace('{$'.$key.'}',$param,$txt);
        }
        dump('http://'.request()->getHost().Storage::disk('audio')->url('result.mp3'));
        dump('http://pvtest.visastandards.com/id_merchant='.base64_encode(strrev(base64_encode(1))));
        dd($txt);
        $res = $tts->transition('生长的亚洲');
        $url = asset('storage/voice/result.mp3');

        return view('test',compact('url'));
    }

    protected $_client;
    protected $_curl;
    protected $_request_url = 'http://yqbassis.test/gclients';//http://yqbassis.com/gclients //https://pv.yaoqianba.com/gclients
    protected $_dev_request_url = 'http://yqbassis.com/gclients';

    public function __construct(Curl $client)
    {
        dd(config('services')['save_voice_time']['exp']);
        $this->_client = $client;
        $this->_curl = curl_init();
        curl_setopt($this->_curl, CURLOPT_URL, $this->_request_url);
        curl_setopt($this->_curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($this->_curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($this->_curl, CURLOPT_POST, 1);
        curl_setopt($this->_curl, CURLOPT_HTTPHEADER, array());
        curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, 1);
    }


    public function info(Request $request)
    {
        $data = json_encode([
            'biz_type' => 'merchant.minilogin',
            'biz_content' => [
                'wxmini_code' => '1',
            ],
        ]);
        $sign = '';
        curl_setopt($this->_curl, CURLOPT_POSTFIELDS, array(
            'data' => $data,
//        ''
        ));
        $ret = curl_exec($this->_curl);
        dump($ret);
        dump(json_decode($ret,true));
        echo '<br /><br />';
        die();
    }

    public function tts(Request $request)
    {
        $data = json_encode([
            'biz_type' => 'voicenotify.mini',
            'biz_content' => [
                'id_merchant' => '446',
                'type' => 'PAY_SUCCESS',
                'order' => [],
                'template_id' => '10001',
                'template_params' => [
                    'amt' => 10
                ],
            ],
        ]);
        dump($data);

        $sign = '';
        curl_setopt($this->_curl, CURLOPT_POSTFIELDS, array(
            'data' => $data,
            'sign' => $sign
        ));
        $ret = curl_exec($this->_curl);
        var_dump($ret);
        dump(json_decode($ret,true));
        echo '<br /><br />';
        die();
    }

    public function order()
    {
        $data = json_encode([
            'biz_type' => 'voicenotify.fetch',
            'biz_content' => [
                'id_merchant' => '446',
                'type' => 'PAY_SUCCESS',
            ],
        ]);
        $sign = '';
        dump($data);
        curl_setopt($this->_curl, CURLOPT_POSTFIELDS, array(
            'data' => $data,
            'sign' => $sign
        ));
        $ret = curl_exec($this->_curl);
        dump($ret);
        dump(json_decode($ret,true));
        echo '<br /><br />';
        die();
    }

    public function setCache(BaiduTtsApi $ttsApi, Request $request)
    {
        $id_merchant = $request->get('id_merchant',446);
        $amt = mt_rand(10,20);
        $audio_data = $ttsApi->getAudioSrc('10001',['amt' => $amt]);

dump($audio_data);
        if($audio_data['voice_src']){
            $src_arr = \Cache::get('PAY_SUCCESS_446')??[];
            dump(date('Y-m-d'));
            $order = [
                'id_merchant' => $id_merchant,
                'id_wxuser' => '1957',
                'amount_due' => $amt*100,
                'amount' => $amt*100,
                'discount' => '0.2',
                'mchreturn' => '0.2',
                'out_tradeno' => time().mt_rand(1,99999),
                'channel_tradeno' => '',
                'trade_status' => '1',
                'allpay_biz_channel' => 'wx',
                'allpay_biz_type' => 'wx.jspay',
                'allpay_merchantno' => '86570004',
                'allpay_terminalno' => '',
                'paytime' => date('Y-m-d H:i:s'),
                'createtime' => date('Y-m-d H:i:s'),
                'fee' => '0',
                'is_subscribe' => '0',
                'is_get_redpack' => '0',
            ];
            $teadedetail_model = TradeDetail::create($order);
            $today_sum = TradeDetail::where('id_merchant',$id_merchant)->where('createtime', 'like', '%'.date('Y-m-d').'%')->sum('amount_due');
dump($teadedetail_model);
            $today_sum = round($today_sum/100,2);
            $order_detail = TradeDetail::where('id_tradedetail',$teadedetail_model->id_tradedetail)->with('wxuser')->first();

            if($order_detail){
                $order_data = [
                    'paytime_text' => $order_detail->paytime,
                    'amount_due' => $order_detail->amount_due,
                    'out_tradeno' => $order_detail->out_tradeno,
                    'wxuser' => [
                        'nickname' => $order_detail->wxuser->nickname,
                        'avatar_url' => $order_detail->wxuser->avatar_url,
                    ],
                ];
            }

            $new_data = [
                'type' => 'PAY_SUCCESS',
                'voice_src' => $audio_data['voice_src'],
                'today_sum' => $today_sum,
                'rel_text' => $audio_data['rel_text'],
                'order' => $order_data??[],
            ];
            array_push($src_arr,$new_data);
dump($new_data);

            \Cache::forever('PAY_SUCCESS_446' ,$src_arr);

        }

        dd(\Cache::get('PAY_SUCCESS_446'));
    }
}
